package com.oocl.command;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.oocl.server.po.Deptment;
import com.oocl.server.po.Person;

public class KeyValue {
	private static String line;
	
	public KeyValue(String line) {
		super();
		this.line = line;
	}

	public int getId() {
		String tempLine = line.split(" ")[1];
		String tempLine2 = tempLine.split(";")[0];
		int id = Integer.parseInt(tempLine2.split(":")[1]);
		return id;
	}

	public Person setPersonObject() throws ParseException {
		Person person = new Person();
		String tempLine = line.split(" ")[1];
		String[] itemMaps = tempLine.split(",|;");

		for (String itemMap : itemMaps) {
			String value = itemMap.split(":")[1];
			if (itemMap.contains("pid")) {
				person.setPid(Integer.parseInt(value));
			}
			if (itemMap.contains("pname")) {
				person.setPname(value);
			}
			if (itemMap.contains("birthday")) {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				java.util.Date uDate = dateFormat.parse(value);
				Date sqlDate = new Date(uDate.getTime());
				person.setBirthday(sqlDate);
			}
			if (itemMap.contains("tel")) {
				person.setTel(value);
			}
			if (itemMap.contains("did")) {
				person.setDid(Integer.parseInt(value));
			}
			if (itemMap.contains("salary")) {
				person.setSalary(Integer.parseInt(value));
			}
		}

		return person;
	}

	public Deptment setDeptObject() {
		Deptment dept = new Deptment();
		String tempLine = line.split(" ")[1];
		String[] itemMaps = tempLine.split(",");

		for (String itemMap : itemMaps) {
			String value = itemMap.split(":")[1];
			 if (itemMap.contains("did")) {
				 dept.setDid(Integer.parseInt(value));
			 }
			if (itemMap.contains("dname")) {
				dept.setDname(value);
			}
			if (itemMap.contains("city")) {
				dept.setCity(value);
			}
		}

		return dept;
	}
}
